<template>
  <div class="company_edit">
    <el-card class="box-card">
      <div class="title">
        <span>修改用户</span>
      </div>
      <div class="detail">
        <el-form :model="edit" :rules="rules" ref="ruleForm" label-width="auto">
          <el-row>
            <el-col :span="24">
              <el-form-item label="公司名称" prop="company_name">
                <el-input
                  v-model="edit.company_name"
                  clearable
                  style="width: 93%"
                ></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="负责人" prop="company_director">
                <el-input v-model="edit.company_director" clearable></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="联系手机" prop="company_tel">
                <el-input v-model="edit.company_tel" clearable></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="邮箱" prop="company_email">
                <el-input v-model="edit.company_email" clearable></el-input>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
        <div class="btn">
          <el-button icon="el-icon-arrow-left" @click="back">返回</el-button>
          <el-button
            type="primary"
            @click="submitForm('ruleForm')"
            style="margin-left: 50px"
            >提交<i class="el-icon-upload el-icon--right"></i
          ></el-button>
        </div>
      </div>
    </el-card>
  </div>
</template>

<script>
export default {
  name: "Company_Edit",
  inject: ["companyRefresh"],
  props: ["companyData"],
  data() {
    return {
      edit: {
        company_id: "",
        company_name: "",
        company_director: "",
        company_tel: "",
        company_email: "",
      },
      rules: {
        company_name: [
          { required: true, message: "请输入合作单位", trigger: "blur" },
        ],
        company_director: [
          { required: true, message: "请输入负责人", trigger: "blur" },
        ],
        company_tel: [
          { required: true, message: "请输入联系电话", trigger: "blur" },
        ],
        company_email: [
          { required: true, message: "请输入邮箱", trigger: "blur" },
        ],
      },
    };
  },
  mounted() {
    const id = this.$route.params.id;
    let result = this.companyData.find((item) => {
      if (item.company_id == id) {
        return item;
      }
    });
    delete result.create_time;
    delete result.modify_time;
    delete result.status;
    this.edit = result;
  },
  methods: {
    back() {
      this.$store.commit("maskStatus_change", false);
      this.$router.go(-1);
      this.pageRefresh();
    },
    //刷新页面
    pageRefresh() {
      this.companyRefresh();
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.axios
            .post("/company/changeInfo", this.edit, {
              "Content-Type": "application/json",
            })
            .then((res) => {
              if (res.data.code === "200") {
                this.$message({
                  message: res.data.msg,
                  type: "success",
                });
                this.back();
              } else {
                this.$message.error(res.data.msg);
              }
            })
            .catch((err) => {
              console.error(err);
              this.$message.error("修改合作单位失败");
            });
        } else {
          this.$message({
            message: "请完整填写修改信息",
            type: "warning",
          });
          return false;
        }
      });
    },
  },
};
</script>

<style scoped>
.box-card {
  position: absolute;
  left: 30%;
  top: 15%;
  width: 880px;
  height: 500px;
  z-index: 999;
}
.title {
  width: 95%;
  height: 5vh;
  padding: 0 0 0 30px;
  line-height: 3vh;
  text-align: left;
  font-size: 1.2rem;
  font-weight: 600;
  border-bottom: 1px dashed #c4c6c7;
}
.title span {
  display: inline-block;
  width: 80%;
  height: 55%;
  padding: 0px 20px;
  border-left: 5px solid #3b83cc;
}
.detail {
  margin: 60px auto;
  width: 80%;
}

.detail /deep/ .el-form-item__label {
  font-weight: 550;
}
.detail /deep/ .el-input {
  width: 85%;
}
.detail /deep/ .el-form-item__error {
  margin-left: 8%;
}
.detail /deep/ .el-row {
  margin-top: 20px;
}
.btn {
  position: absolute;
  left: 50%;
  bottom: 10%;
  transform: translate(-50%, 0);
  margin-bottom: 0;
}
</style>